package dht.data;

import java.io.IOException;
import java.util.Random;
import java.util.TreeMap;
import java.util.Vector;

import dht.simulation.GlobalVariables;
import dse.Network;
import dse.modules.ZipfDistribution;

public class FileAccessGenerator {

	protected Random ran;
	protected AccessGenerator accessGen;
	protected int accessIdOffset;
	protected TreeMap<Integer, String> files;
	protected ZipfDistribution zipfDist;

	public FileAccessGenerator(int fileNumber) throws IOException {
		ran = new Random(GlobalVariables.seed);
		accessGen = new AccessGenerator();
		accessIdOffset = 0;
		FileGenerator fileGen = new FileGenerator();
		files = fileGen.generate(fileNumber);
		zipfDist = new ZipfDistribution(0.7, fileNumber);
	}

	public Vector<Access> generate(Network network, int lambdaR, int lambdaW,
			double totalTime) {
		Vector<Access> res = new Vector<Access>();
		TreeMap<Double, AccessType> access = accessGen.generate(lambdaR,
				lambdaW, totalTime);
		for (Double key : access.keySet()) {
			int rank = zipfDist.getRank(ran.nextDouble());
			Access ac = new Access();
			ac.accessId = ++accessIdOffset;
			ac.startTime = key;
			ac.requesterId = ran.nextInt(network.maxNodeId()) + 1;
			ac.fileName = files.get(rank);
			ac.accessType = access.get(key);
			// if (ac.accessType == AccessType.WRITE) {
			ac.fileSize = GlobalVariables.fileBlockSize
					* (ran.nextInt(GlobalVariables.maxFileBlockNumber) + 1);
			// }
			res.add(ac);
		}
		// System.out.println(accessIdOffset);
		return res;
	}

	public static void main(String[] args) throws IOException {

	}

}
